//-----------------------------------------------------------------------
//<copyright file="BaseEntity.cs" >
//Copyright (c) MinhSong. .
//</copyright>
//-----------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Collections;

namespace PMS.Entity
{
    [Serializable]
    public class BaseEntity : IBaseEntity
    {
        #region Common properties
        private byte[] m_DataVersion;
        private string m_CreatedBy;
        private DateTime m_CreatedDate;
        private string m_UpdatedBy;
        private DateTime m_UpdatedDate;     
        #endregion 

        public int RowStatus
        {
            get;
            set;
        }

        [DataMember]
        public byte[] DataVersion
        {
            get { return m_DataVersion; }
            set
            {
                m_DataVersion = value;
                OnPropertyChanged("DataVersion");
            }
        }

        /// <summary>
        ///
        /// </summary>
        ///<value>This type is string</value>
        [DataMember]
        public string CreatedBy
        {
            get { return m_CreatedBy; }
            set
            {
                m_CreatedBy = value;
                OnPropertyChanged("CreatedBy");
            }
        }

        /// <summary>
        ///
        /// </summary>
        ///<value>This type is DateTime</value>
        [DataMember]
        public DateTime CreatedDate
        {
            get { return m_CreatedDate; }
            set
            {
                m_CreatedDate = value;
                OnPropertyChanged("CreatedDate");
            }
        }

        /// <summary>
        ///
        /// </summary>
        ///<value>This type is string</value>
        [DataMember]
        public string UpdatedBy
        {
            get { return m_UpdatedBy; }
            set
            {
                m_UpdatedBy = value;
                OnPropertyChanged("UpdatedBy");
            }
        }

        /// <summary>
        ///
        /// </summary>
        ///<value>This type is DateTime</value>
        [DataMember]
        public DateTime UpdatedDate
        {
            get { return m_UpdatedDate; }
            set
            {
                m_UpdatedDate = value;
                OnPropertyChanged("UpdatedDate");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public object Clone()
        {
            var type = this.GetType();
            var newInstance = Activator.CreateInstance(type);

            foreach (var p in type.GetProperties())
            {
                var value = p.GetValue(this, null);

                if (value is ICloneable)
                {
                    p.SetValue(newInstance, ((ICloneable)value).Clone(), null);
                }
                p.SetValue(newInstance, value, null);
            }

            return newInstance;
        }
    }

    public enum RowStatus
    {
        DoNothing = 0,
        Update = 1,
        Delete = 2,
        New = 3
    }
}
